<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams">
        <el-row>
          <el-col :span="3">
            <el-form-item label="入库批次编号：" prop="wxfwCode">
              <el-input clearable maxlength="100" placeholder="请输入入库批次编号" v-model="queryParams.wxfwCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="容器/包装编码：" prop="containerCode" label-width="115">
              <el-input clearable maxlength="100" placeholder="请输入" v-model="queryParams.containerCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="危险品俗称/名录名称：" prop="wxfwName" label-width="150">
              <el-input clearable maxlength="100" placeholder="请输入" v-model="queryParams.wxfwName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="危废类别：" prop="wxfwType" label-width="80">
              <el-select placeholder="请选择" clearable v-model="queryParams.wxfwType" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(wxfw_type)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="危废代码：" prop="status" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入危废代码" v-model="queryParams.wxfwName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="入库日期：" prop="createDate" label-width="80">
              <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" :clearable="false"
                :editable="false" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="入库人：" prop="wxfwUser" label-width="60">
              <el-input clearable maxlength="100" placeholder="请输入" v-model="queryParams.wxfwUser"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-row justify="center">
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="resetBtn" @click="resetQuery">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation" style="margin-top: 9.2px">
          危险废物库存记录列表
        </p>
        <p class="totalStyle" style="padding-top: 6.2px">
          共 {{ total }} 条数据
        </p>
        <!-- <el-button type="success" link icon="Plus" style="padding-bottom: 18px; margin-left: 10px">扫码出库</el-button>
        <el-form-item label="扫码结果：" style="margin-left: 10px">
          <el-input clearable maxlength="100"></el-input>
        </el-form-item> -->
        <!-- <el-button type="success" link icon="Plus" style=" margin-left: 10px">批量出库</el-button> -->
        <el-table ref="mainTableRef" :data="wxfwTable" v-loading="loading">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column show-overflow-tooltip label="入库批次编号" prop="wxfwCode" align="center" width="110" />
          <el-table-column show-overflow-tooltip label="入库时间" prop="createDate" align="center" width="100" />
          <el-table-column show-overflow-tooltip label="容器/包装编码" prop="containerCode" align="center" width="120" />
          <el-table-column show-overflow-tooltip label="容器/包装类型" prop="containerTypeName" align="center" width="120" />
          <el-table-column show-overflow-tooltip label="容器/包装数量" prop="containerCount" align="center" width="120" />
          <el-table-column show-overflow-tooltip label="内部名称" prop="internalName" align="center" />
          <el-table-column show-overflow-tooltip label="国家名录名称" prop="coudirName" align="center" />
          <el-table-column show-overflow-tooltip label="危废类别" prop="wxfwTypeName" align="center" width="80" />
          <el-table-column show-overflow-tooltip label="危废代码" prop="wxfwNumber" align="center" width="80" />
          <el-table-column show-overflow-tooltip label="入库量" prop="createCount" align="center" width="80" />
          <el-table-column show-overflow-tooltip label="计量单位" prop="unitName" align="center">
            <template #default="scope">
              <span v-html="transformation(scope.row.unitName)"></span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="入库运送经办人" prop="transportUser" align="center" />
          <el-table-column show-overflow-tooltip label="贮存部门经办人" prop="storageUser" align="center" />
          <el-table-column show-overflow-tooltip label="产生批次编码" prop="batchCode" align="center" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
            <template #default="scope">
              <el-button link type="success" @click="handleUse(scope.row)">出库</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <!-- 单个入库和引用弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      @close="closedInDialog" v-model="wfOutOpen" title="危废出库" width="1600">
      <el-form label-width="130" ref="rkOrYyRef" :model="form" :rules="rules">
        <el-row>
          <el-col :span="6">
            <el-form-item label="处置库批次编号：" prop="handleCode">
              <el-input clearable maxlength="100" v-model="form.handleCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="出厂日期：" prop="handleDate">
              <el-date-picker v-model="form.handleDate" placeholder="请选择出厂日期" type="date" style="width: 100%"
                value-format="YYYY-MM-DD" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="去向：" prop="destination">
              <el-input clearable maxlength="100" v-model="form.destination"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="处置方式：" prop="handleType">
              <el-input clearable maxlength="100" v-model="form.handleType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="接收单位类型：" prop="companyType">
              <el-input clearable maxlength="100" v-model="form.companyType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="接收单位名称：" prop="companyName">
              <el-input clearable maxlength="100" v-model="form.companyName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="许可证编号：" prop="companyCode">
              <el-input clearable maxlength="100" v-model="form.companyCode"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <p class="pinformation">出库列表</p>
          <p class="totalStyle">共 {{ form.handleList.length }} 条数据</p>
        </el-row>
        <el-table :data="form.handleList">
          <el-table-column show-overflow-tooltip label="容器/包装编码" prop="containerCode" align="center" />
          <el-table-column show-overflow-tooltip label="容器/包装类型" prop="containerType" align="center">
            <template #default="scope">
              <dict-tag :options="container_type" :value="scope.row.containerType" />
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="容器/包装数量" prop="containerCount" align="center" />
          <el-table-column show-overflow-tooltip label="内部名称" prop="internalName" align="center" />
          <el-table-column show-overflow-tooltip label="国家名录名称" prop="coudirName" align="center" />
          <el-table-column show-overflow-tooltip label="危废类别" prop="wxfwType" align="center">
            <template #default="scope">
              <dict-tag :options="wxfw_type" :value="scope.row.wxfwType" />
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="危废代码" prop="wxfwNumber" align="center" />
          <el-table-column show-overflow-tooltip label="出库量" prop="createCount" align="center">
            <template #default="scope">
              <el-input-number clearable placeholder="请输入出库量" :min="1" style="width: 100%" controls-position="right"
                v-model="scope.row.handleCount" />
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="计量单位" prop="unitName" align="center">
            <template #default="scope">
              <span v-html="transformation(scope.row.unitName)"></span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="入库批次编号" prop="batchCode" align="center" />
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="saveWxfw">确 定</el-button>
          <el-button type="danger" @click="wfOutOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WxfwOutin">
import { listLedger, handleSave } from "@/api/resource/wxfw";

const { proxy } = getCurrentInstance();
const { wxfw_type, container_type } = proxy.useDict(
  "wxfw_type",
  "container_type"
);

const wxfwTable = ref([]);
const wfOutOpen = ref(false);
const loading = ref(false);
const data = reactive({
  form: { handleList: [] },
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
    wxfwType: -1,
    status: -1,
  },
  rules: {
    companyCode: [{ required: true, message: "许可证编号不能为空", trigger: "blur" }],
    handleType: [{ required: true, message: "处置方式不能为空", trigger: "blur" }],
    companyType: [{ required: true, message: "接受单位类型不能为空", trigger: "blur" }],
    companyName: [{ required: true, message: "接收单位名称不能为空", trigger: "blur" }],
    handleCode: [{ required: true, message: "处置库批次编号不能为空", trigger: "blur" }],
    handleDate: [{ required: true, message: "出厂日期不能为空", trigger: "blur" }],
    destination: [{ required: true, message: "去向不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

const total = ref(0);
const dateRange = ref([]);
function getList() {
  loading.value = true;
  listLedger(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (res) => {
      wxfwTable.value = res.rows;
      loading.value = false;
      total.value = res.total;
    }
  );
}
getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

function closedInDialog() {
  form.value = {
    handleList: [],
  };
  proxy.$refs["rkOrYyRef"]?.clearValidate();
}

function saveWxfw() {
  proxy.$refs["rkOrYyRef"].validate((valid) => {
    if (valid) {
      handleSave(form.value).then((res) => {
        wfOutOpen.value = false;
        proxy.$modal.msgSuccess(res.msg);
        getList();
      });
    }
  });
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

function transformation(row) {
  return proxy.getSupAndSub(row);
}

function handleUse(row) {
  form.value.handleList = [JSON.parse(JSON.stringify(row))];
  wfOutOpen.value = true;
}
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
